package com.woniuxy.book.mq;

import cn.hutool.json.JSONUtil;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.woniuxy.book.entity.OrderInfo;
import com.woniuxy.book.entity.OrderRefund;
import com.woniuxy.book.mapper.OrderInfoMapper;
import com.woniuxy.book.mapper.OrderRefundMapper;
import com.woniuxy.book.util.StateData;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;

@Component
public class OrderRefundConsumer {


    @Resource
    private AlipayClient alipayClient;
    @Resource
    private OrderRefundMapper orderRefundMapper;
    @Resource
    private OrderInfoMapper orderInfoMapper;

    @RabbitListener(queuesToDeclare = {@Queue (name = "refund-queue")})
    @RabbitHandler
    public void comsumer(Map<String, Object> refundParam) throws Exception {
        String orderId = (String) refundParam.get("orderId");
        String refundMoney = refundParam.get("refundMoney").toString();
        String refundReason = (String) refundParam.get("refundReason");
        String refundId = (String) refundParam.get("refundId");
        OrderRefund orderRefund = orderRefundMapper.selectById(refundId);
        orderRefund.setRefundState(StateData.ORDER_REFUND_REFUNDING);
        orderRefundMapper.updateById(orderRefund);
        OrderInfo orderInfo = orderInfoMapper.selectById(orderId);
        orderInfo.setOrderState(StateData.ORDER_STATE_REFUNDIND);
        orderInfoMapper.updateById(orderInfo);
        //设置请求参数
        AlipayTradeRefundRequest alipayRequest = new AlipayTradeRefundRequest();
        alipayRequest.setBizContent("{\"out_trade_no\":\""+ orderId +"\","
                + "\"refund_amount\":\""+ refundMoney +"\","
                + "\"refund_reason\":\""+ refundReason +"\","
                + "\"out_request_no\":\""+ refundId +"\"}");
        //请求
        String result = alipayClient.execute(alipayRequest).getBody();
        System.out.println(result);
        //输出
        String refundInfo = JSONUtil.parseObj(result).get("alipay_trade_refund_response").toString();
        String refundCode = JSONUtil.parseObj(refundInfo).get("code").toString();
        String msg = JSONUtil.parseObj(refundInfo).get("msg").toString();
        String refundTime = JSONUtil.parseObj(refundInfo).get("gmt_refund_pay").toString();
        if ("10000".equals(refundCode)) {
            if ("Success".equals(msg)) {
                orderRefund.setRefundState(StateData.ORDER_REFUND_SUCCESS);
                orderRefund.setRefundTime(LocalDateTime.parse(refundTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                orderRefundMapper.updateById(orderRefund);
                orderInfo.setOrderState(StateData.ORDER_STATE_REFUNDED);
                orderInfoMapper.updateById(orderInfo);
            }
        }
    }
}
